package cn.elves.model.rule.controller;

import cn.elves.model.entity.dto.response.ResponseDTO;
import cn.elves.model.rule.entity.dto.RuleTreeDTO;
import cn.elves.model.rule.entity.po.RuleTree;
import cn.elves.model.rule.service.RuleTreeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController("drawing")
public class RuleTreeController {

    @Resource
    private RuleTreeService ruleTreeService;

    @GetMapping("/ruleTree/page/{pageSize}/{pageNum}")
    public ResponseDTO<Page<RuleTree>> ruleTreePage(@PathVariable("pageSize") Integer pageSize, @PathVariable("pageNum") Integer pageNum) {
        return ResponseDTO.success(ruleTreeService.page(new Page<>(pageSize, pageNum)));
    }


    @GetMapping("/ruleTree/{id}")
    public ResponseDTO<RuleTree> selectRuleTree(@PathVariable("id") Long id) {
        return ResponseDTO.success(ruleTreeService.getById(id));
    }

    @PostMapping("/ruleTree")
    public ResponseDTO<RuleTree> saveRuleTree(@RequestBody RuleTreeDTO ruleTree) {
        ruleTreeService.saveRuleTree(ruleTree);
        return ResponseDTO.success();
    }

    @PutMapping("/ruleTree")
    public ResponseDTO<RuleTree> updateRuleTree(@RequestBody RuleTreeDTO ruleTree) {
        ruleTreeService.updateRuleTree(ruleTree);
        return ResponseDTO.success();
    }

    @GetMapping("/addRule")
    public ResponseDTO<RuleTree> addRule(@RequestParam("ruleKey") String ruleKey) throws Exception {
        ruleTreeService.addRule(ruleKey);
        return ResponseDTO.success();
    }


}
